package com.gmall.product.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gmall.common.result.Result;
import com.gmall.model.product.BaseCategory;
import com.gmall.model.product.dto.BaseCategoryDTO;
import com.gmall.model.product.dto.Category123IdNameDTO;
import com.gmall.product.mapper.BaseCategoryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryService {

    @Autowired
    BaseCategoryMapper baseCategoryMapper;

    public Result<List<BaseCategoryDTO>> findCategoryTree() {
        List<BaseCategoryDTO> categoryTree = baseCategoryMapper.findCategoryTree();

        return Result.ok(categoryTree);
    }

    public Result<List<BaseCategory>> getCategory1() {

        QueryWrapper<BaseCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseCategory::getParentID, 0);
        List<BaseCategory> baseCategories = baseCategoryMapper.selectList(queryWrapper);

        return Result.ok(baseCategories);
    }

    public Result<List<BaseCategory>> getCategory2(Long category1Id) {

        QueryWrapper<BaseCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseCategory::getParentID, category1Id);
        List<BaseCategory> baseCategories = baseCategoryMapper.selectList(queryWrapper);

        return Result.ok(baseCategories);
    }

    public Result<List<BaseCategory>> getCategory3(Long category2Id) {
        QueryWrapper<BaseCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseCategory::getParentID, category2Id);
        List<BaseCategory> baseCategories = baseCategoryMapper.selectList(queryWrapper);

        return Result.ok(baseCategories);
    }

    public Result<List<BaseCategoryDTO>> findCategoryTreeStream() {
        List<BaseCategory> baseCategoryList = baseCategoryMapper.selectList(null);

        List<BaseCategoryDTO> collect = baseCategoryList.stream().filter((baseCategory -> {
            return baseCategory.getParentID().equals(0L);//找出第一级分类
        })).map((baseCategory) -> {
            BaseCategoryDTO baseCategoryDTO = new BaseCategoryDTO();
            BeanUtils.copyProperties(baseCategory, baseCategoryDTO);

            baseCategoryDTO.setChildren(getChildren(baseCategoryList,baseCategory.getId()));//找出第二级分类
            return baseCategoryDTO;
        }).collect(Collectors.toList());

        return Result.ok(collect);
    }

    private List<BaseCategoryDTO> getChildren(List<BaseCategory> baseCategoryList,Long id) {

        List<BaseCategoryDTO> collect = baseCategoryList.stream().filter((baseCategory) -> {
            return baseCategory.getParentID().equals(id);
        }).map((baseCategory) -> {
            BaseCategoryDTO baseCategoryDTO = new BaseCategoryDTO();
            BeanUtils.copyProperties(baseCategory, baseCategoryDTO);

            baseCategoryDTO.setChildren(getChildren(baseCategoryList, baseCategory.getId()));//找出第三级分类,迭代
            return baseCategoryDTO;
        }).collect(Collectors.toList());
        if(collect.size()==0){
            return null;
        }
        return collect;

    }

    public Result<Category123IdNameDTO> findCategory123IdNameDTOByCategory3Id(Long category3Id) {
        Category123IdNameDTO category123IdNameDTO = baseCategoryMapper.findCategory123IdNameDTOByCategory3Id(category3Id);
        return Result.ok(category123IdNameDTO);
    }
}
